package com.wxy.xr.servlet;

import com.wxy.xr.bean.Skill;
import org.apache.commons.beanutils.BeanUtils;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

@WebServlet("/skill/*")
public class SkillServlet extends BaseServlet<Skill>{
    public  void admin(HttpServletRequest request, HttpServletResponse response) throws Exception{
        request.setAttribute("skills",service.list());
        forward(request, response,"admin/skill.jsp");
    }

    public  void save(HttpServletRequest request, HttpServletResponse response) throws Exception{
        Skill skill=new Skill();
        BeanUtils.populate(skill,request.getParameterMap());
        if(service.save(skill)){
            redirect(request, response,"skill/admin");
        } else {
            forwardError(request, response,"专业技能保存失败");
           }
    }

    public  void remove(HttpServletRequest request, HttpServletResponse response) throws Exception{
        String[] idstrs=request.getParameterValues("id");
        List<Integer> ids=new ArrayList<>();
        for(String idstr:idstrs){
            ids.add(Integer.valueOf(idstr));
        }

        if(service.remove(ids)){
            redirect(request, response,"skill/admin");
        }else{
            forwardError(request, response,"专业技能删除失败");}
    }
}
